using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PowersourceUIToolkit;
using UCL.Powersource;
using UCL.Powersource.Mock;
using UCL.Powersource.TFS;

namespace Powersource_UnitTests.TestBootstrappers
{

    public static class LogHelper
    {
        public static ILog CreateTestLog(TestContext testContext)
        {
            //StackTrace st = new StackTrace(skipframes, true);
            var name = testContext.TestName;
            var tempDir = Environment.GetEnvironmentVariable("TEMP") + "\\";
            var flog=new FileLog(VerbosityLevels.Debug, string.Format("{2}\\Powersource-{0:yyyyMMdd}-{1}.log", DateTime.Now, name, tempDir),false);
            LogManager.DefaultInstance.AddLog(flog);
            LogManager.DefaultInstance.Info("Log {0} added at {1}", flog, DateTime.Now);
            return LogManager.DefaultInstance;
        }
    }

    public static class BootStrapperLoader
    {

        //public static TFS_CNSTR_Powersource = "";

        private static Bootstrapper BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS_DummyBridge;
        private static Bootstrapper BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS;
        private static Bootstrapper BS_DummyAll;

        
        
        public static DummyPowersourceBootStrapper CreateDummyVisualStudio_RealStatMan_RealRepMan_RealTFS_DummyBridge()
        {
            if (BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS_DummyBridge != null && !BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS_DummyBridge.IsDisposed) throw new InvalidOperationException("Bootstrapper already initialized, cannot create new bootstrapper unless the old one is disposed!");
            
            var bs = new DummyPowersourceBootStrapper();
            bs.TypesToLoad = new Type[] { typeof(UCL.Powersource.ViewModels.PowersourceMainViewModel) };
            bs.UIManager = new PowersourceUIToolkit.UIManager();
            bs.TfsTeamProjectCollectionFactory = new TfsTeamProjectCollectionFactoryAdapter();
            bs.PackageConfigurationFactory = new PackageConfigurationFactory();
            bs.StatisticManager = new StatisticManager();
            bs.ReportingManager = new ReportingManager();
            bs.StatisticManagerQueue = new StatisticManagerQueue();
            bs.RemoteBuilderQueue = new RemoteBuilderQueue();
            bs.IVsEnv = new DummyVSEnv() { Uri = "http://81.2.210.131:8080/tfs/TestProjectCollection1/" };
            bs.Initialize();
            BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS_DummyBridge = bs;
            return bs;
        }


        public static DummyPowersourceBootStrapper CreateDummyVisualStudio_RealStatMan_RealRepMan_RealTFS(IEnumerable<Type> extratypesToLoad=null)
        {
            if (BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS != null && !BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS.IsDisposed) throw new InvalidOperationException("Bootstrapper already initialized, cannot create new bootstrapper unless the old one is disposed!");

            var bs = new DummyPowersourceBootStrapper();
            List<Type> lsTypes = new List<Type>();
            if (extratypesToLoad != null)
            {
                lsTypes.AddRange(extratypesToLoad);
            }
            lsTypes.Add(typeof(UCL.Powersource.ViewModels.PowersourceMainViewModel));
            bs.TypesToLoad = lsTypes.ToArray();
            bs.DefaultInterfaceImplementationTypes.Add(typeof(IAnalysisBridge), typeof(AnalysisBridge));
            bs.UIManager = new PowersourceUIToolkit.UIManager();
            bs.TfsTeamProjectCollectionFactory = new TfsTeamProjectCollectionFactoryAdapter();
            bs.PackageConfigurationFactory = new PackageConfigurationFactory();
            bs.StatisticManager = new StatisticManager();
            bs.ReportingManager = new ReportingManager();
            bs.StatisticManagerQueue = new StatisticManagerQueue();
            bs.RemoteBuilderQueue = new RemoteBuilderQueue();
            bs.IVsEnv = new DummyVSEnv() { Uri = "http://81.2.210.131:8080/tfs/TestProjectCollection1/" };
            bs.Initialize();
            BS_DummyVisualStudio_RealStatMan_RealRepMan_RealTFS = bs;
            return bs;
        }

        public static DummyPowersourceBootStrapper CreateDummyAll()
        {
            if (BS_DummyAll != null && !BS_DummyAll.IsDisposed) throw new InvalidOperationException("Bootstrapper already initialized, cannot create new bootstrapper unless the old one is disposed!");

            var bs = new DummyPowersourceBootStrapper();
            bs.TypesToLoad = new Type[] { typeof(UCL.Powersource.ViewModels.PowersourceMainViewModel) };
            bs.IVsEnv = new DummyVSEnv() { Uri = "http://1.1.1.1:8080/tfs/TestProjectCollection1" };
            bs.Initialize();
            BS_DummyAll = bs;
            return bs;
        }
    }
}
